<?php 
session_start();
$host = '127.0.0.1';
$port = 6379;
$data = file_get_contents("php://input");
$data =json_decode($data, true);
$preg=preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/",$data['to']);
if($data['to']==null || $preg==0 ){
	$arr=[
                'status' => -1,
                'msg' => '不能为空'
            ];
	exit( json_encode($arr));
	
}
 //本次能否发送
        $flag = false;
        if(empty($_SESSION['last_send_time']))
        {
            $flag = true;
        }
        else
        {
            //15秒间隔判断
            $nowTime = time();
            if($nowTime - $_SESSION['last_send_time'] > 15)
            {
                $flag = true;
            }
        }

        //能否发送
        if(!$flag)
        {
	        $arr=[
                'status' => 0,
                'msg' => '请不要重复发送'
            ];
      	    exit( json_encode($arr));
        }
        else
        {
            $_SESSION['last_send_time'] = time();
            $connection = new \Redis();
        	$connection->connect($host, $port);
        	$body=mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9);
        	$queue_key = 'mail_task';
        	$message = [
        	'subject' => '潘潘的邮箱验证码',
        	'body' =>$body,
        	'receiver' =>trim($data['to']),
        	         ];
        	$message = json_encode($message);
        	
        	$connection->lpush($queue_key, $message);
        
        	$arr=[
                        'status' => 1,
                        'msg' => 'ok'
                    ];
        	exit( json_encode($arr));
        }


